{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 江湖据传……\n",
    "* 所谓夫妻相是因为你看惯了自己的脸，习惯了对这一张脸的审美，所以在你遇到与这张脸相似的另一张异性的脸时，你会感到莫名的亲切，会增大一见钟情的可能，促使后来成为夫妻，今天我就想用人脸验证的方法，来验证一下这一说法，看看人脸验证是否认可所谓的夫妻相"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先写一下通用的东西\n",
    "import requests #导入requests模块\n",
    "import json\n",
    "create_facelists_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\" \n",
    "subscription_key = \"9c72e39c790c441b9d498ac86e6e7b22\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、create 创建一个装相片的列表\n",
    "data = {\n",
    "    \"name\": \"list_like\",\n",
    "    \"userData\": \"我的钟意者们\",\n",
    "    \"recognitionModel\": \"recognition_02\"\n",
    "}\n",
    "params = {\n",
    "    # Request parameters    \n",
    "   \n",
    "    \"faceListId\":\"who_like\"\n",
    "    \n",
    "#     'detectionModel': 'detection_01',\n",
    "}\n",
    "create = requests.put(create_facelists_url,headers=headers,params=params,json=data)\n",
    "# 请求成功，返回空字符串\n",
    "create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "create.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 存个脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "subscription_key = \"9c72e39c790c441b9d498ac86e6e7b22\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://img2.imgtn.bdimg.com/it/u=2875275021,1241545079&fm=26&gp=0.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"who_like\",\n",
    "    \"userData\":\"钟汉良\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 封装成函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "    subscription_key = \"9c72e39c790c441b9d498ac86e6e7b22\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"faceListId\":\"who_like\",\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 顺便把查看列表的也给封装了\n",
    "# Get facelist\n",
    "def GetFacelist():\n",
    "    get_facelist_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\"\n",
    "    data = {\n",
    "        \"faceListId\":\"who_like\",\n",
    "    }\n",
    "    r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "    return r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2070811178,544319857&fm=26&gp=0.jpg\",\"罗晋\")#可以先用一个试试水"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 把我想测的脸都添加进来，形成我的图片库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586927455213&di=ac0975885de70648755961a518ba9aba&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2019-02-21%2F5c6e711db76b2.jpg\",\"高伟光\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586927819670&di=80f2e702b3ceebfd197d080ff1f7969d&imgtype=0&src=http%3A%2F%2Fwww.bala.cc%2Fuploads%2Fallimg%2F181205%2F1_181205131421_1.jpg\",\"胡歌\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586927939039&di=ac736aef9d971c85af364c7b66d24d8b&imgtype=0&src=http%3A%2F%2Fupload.taihainet.com%2F2016%2F0922%2F1474533044767.jpeg\",\"张一山\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586928053175&di=5c47bbc0e5e6628c1162d78728cceba4&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ffront%2F337%2Fw700h437%2F20190202%2FMs3Y-hsmkfyn6075536.jpg\",\"宋威龙\")\n",
    "AddFace(\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1140984182,284853180&fm=26&gp=0.jpg\",\"吴亦凡\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586928399661&di=819ae625e6489b9734ebf9c2a0d38e87&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn13%2F126%2Fw480h446%2F20180903%2Fe469-hiqtcan3235732.jpg\",\"岳云鹏\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586928583866&di=1d3c4553c28554f6f562f1e58aa10335&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn10109%2F234%2Fw1059h775%2F20190106%2F52c5-hrfcctm9992600.jpg\",\"王凯\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586928767612&di=ab0a89a0b74fe4d79156d9c9d9e4faee&imgtype=0&src=http%3A%2F%2Fd.ifengimg.com%2Fw600%2Fp0.ifengimg.com%2Fpmop%2F2017%2F0126%2F34E0D56655E3BCA9E43464A2427EEEA0C2549B85_size116_w1920_h1200.jpeg\",\"黄轩\")\n",
    "AddFace(\"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2070811178,544319857&fm=26&gp=0.jpg\",\"郑元畅\")\n",
    "AddFace(\"http://img2.imgtn.bdimg.com/it/u=1993243041,2425858167&fm=26&gp=0.jpg\",\"刘昊然\")\n",
    "AddFace(\"http://inews.gtimg.com/newsapp_bt/0/6760822995/1000\",\"辰亦儒\")\n",
    "AddFace(\"http://n.sinaimg.cn/transform/20141124/cesifvw8282565.jpg\",\"杜江\")\n",
    "AddFace(\"http://b-ssl.duitang.com/uploads/blog/201602/18/20160218200922_5heEK.jpeg\",\"吴磊\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586931323182&di=a75ee2bf6475045cbeb56c447e0b9d89&imgtype=0&src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fnote%2Fl%2Fpublic%2Fp57475371.jpg\",\"李诞\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586931653976&di=6c9507d66e001a7309f76817ddccaee4&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn14%2F277%2Fw640h437%2F20180928%2F2233-hkmwytp6844622.jpg\",\"毛不易\")\n",
    "AddFace(\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586931700274&di=ef4cbb9e92689c91b4ddc064f10fadf6&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181213%2Fabc1ef39690c4333b11a3e58e5fb9c36.jpeg\",\"周深\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 把我的脸搞进去测一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"27f9fd65-b4b0-44af-ae24-3e765b945e3d\", \"faceRectangle\": {\"top\": 131, \"left\": 230, \"width\": 454, \"height\": 454}, \"faceAttributes\": {\"smile\": 0.0, \"headPose\": {\"pitch\": -3.9, \"roll\": 1.9, \"yaw\": 1.0}, \"gender\": \"female\", \"age\": 18.0, \"facialHair\": {\"moustache\": 0.0, \"beard\": 0.0, \"sideburns\": 0.0}, \"glasses\": \"ReadingGlasses\", \"emotion\": {\"anger\": 0.0, \"contempt\": 0.0, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 0.0, \"neutral\": 0.997, \"sadness\": 0.003, \"surprise\": 0.0}, \"blur\": {\"blurLevel\": \"low\", \"value\": 0.0}, \"exposure\": {\"exposureLevel\": \"goodExposure\", \"value\": 0.68}, \"noise\": {\"noiseLevel\": \"medium\", \"value\": 0.49}, \"makeup\": {\"eyeMakeup\": true, \"lipMakeup\": true}, \"accessories\": [{\"type\": \"glasses\", \"confidence\": 0.99}], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.17, \"invisible\": false, \"hairColor\": [{\"color\": \"black\", \"confidence\": 0.89}, {\"color\": \"brown\", \"confidence\": 0.85}, {\"color\": \"blond\", \"confidence\": 0.37}, {\"color\": \"other\", \"confidence\": 0.35}, {\"color\": \"gray\", \"confidence\": 0.32}, {\"color\": \"red\", \"confidence\": 0.13}]}}}]'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://coco022.gitee.io/coco022/assets/images/%E5%A4%B4%E5%83%8F.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "json.dumps(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_face = response.json()[0][\"faceId\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 找与我相似的脸（也就是所谓夫妻相）\n",
    "* 把人脸库中与我的脸比对后的结果进行输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#4、find similars\n",
    "\n",
    "findsimilars_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":my_face,\n",
    "    \"faceListId\":\"who_like\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)\n",
    "r_findsimilars.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '6146359b-3cbe-4667-a6a4-f549f2d555d6',\n",
       "  'confidence': 0.14591},\n",
       " {'persistedFaceId': '9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74',\n",
       "  'confidence': 0.10903},\n",
       " {'persistedFaceId': '4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4',\n",
       "  'confidence': 0.10178},\n",
       " {'persistedFaceId': '8704bc6c-39b9-453e-9f6e-1a122b8e5c49',\n",
       "  'confidence': 0.09899},\n",
       " {'persistedFaceId': '941f4a2c-1029-4d30-a46b-084825dfedba',\n",
       "  'confidence': 0.097},\n",
       " {'persistedFaceId': '9146ef76-b10e-49f6-adc7-6684b04457e8',\n",
       "  'confidence': 0.097},\n",
       " {'persistedFaceId': '7f076845-ff16-4aeb-b275-e7a58ac551bc',\n",
       "  'confidence': 0.09519},\n",
       " {'persistedFaceId': '693b6601-2624-4ad8-89f0-d6fd3442b3b9',\n",
       "  'confidence': 0.09388},\n",
       " {'persistedFaceId': '35778304-a5b8-4186-ac3f-5f41e62dd810',\n",
       "  'confidence': 0.09346},\n",
       " {'persistedFaceId': '36583e77-56a3-4e68-8677-60b221e62e56',\n",
       "  'confidence': 0.09098}]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 把结果用pandas处理一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '941f4a2c-1029-4d30-a46b-084825dfedba',\n",
       "   'userData': '罗晋'},\n",
       "  {'persistedFaceId': '7f076845-ff16-4aeb-b275-e7a58ac551bc',\n",
       "   'userData': '高伟光'},\n",
       "  {'persistedFaceId': '2c45febb-8c83-4e4f-a033-eba94eb2e12a',\n",
       "   'userData': '胡歌'},\n",
       "  {'persistedFaceId': 'fa443cd3-3e80-4e15-b0a7-08282f731859',\n",
       "   'userData': '张一山'},\n",
       "  {'persistedFaceId': '3b4bbfef-fbff-4eab-848a-cb9e9fc0f429',\n",
       "   'userData': '宋威龙'},\n",
       "  {'persistedFaceId': '35778304-a5b8-4186-ac3f-5f41e62dd810',\n",
       "   'userData': '吴亦凡'},\n",
       "  {'persistedFaceId': '36583e77-56a3-4e68-8677-60b221e62e56',\n",
       "   'userData': '岳云鹏'},\n",
       "  {'persistedFaceId': '9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74',\n",
       "   'userData': '王凯'},\n",
       "  {'persistedFaceId': 'eb471796-0860-4887-9719-212908594d66',\n",
       "   'userData': '黄轩'},\n",
       "  {'persistedFaceId': '9146ef76-b10e-49f6-adc7-6684b04457e8',\n",
       "   'userData': '郑元畅'},\n",
       "  {'persistedFaceId': '8704bc6c-39b9-453e-9f6e-1a122b8e5c49',\n",
       "   'userData': '辰亦儒'},\n",
       "  {'persistedFaceId': '4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4',\n",
       "   'userData': '杜江'},\n",
       "  {'persistedFaceId': '6146359b-3cbe-4667-a6a4-f549f2d555d6',\n",
       "   'userData': '吴磊'},\n",
       "  {'persistedFaceId': '46cd6146-563e-4b1f-88d9-7d60e3217980',\n",
       "   'userData': '李诞'},\n",
       "  {'persistedFaceId': '693b6601-2624-4ad8-89f0-d6fd3442b3b9',\n",
       "   'userData': '周深'}],\n",
       " 'faceListId': 'who_like',\n",
       " 'name': 'list_like',\n",
       " 'userData': '我的钟意者们'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\"\n",
    "\n",
    "data = {\n",
    "    \"faceListId\":\"who_like\",\n",
    "\n",
    "}\n",
    "\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>941f4a2c-1029-4d30-a46b-084825dfedba</td>\n",
       "      <td>罗晋</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7f076845-ff16-4aeb-b275-e7a58ac551bc</td>\n",
       "      <td>高伟光</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2c45febb-8c83-4e4f-a033-eba94eb2e12a</td>\n",
       "      <td>胡歌</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fa443cd3-3e80-4e15-b0a7-08282f731859</td>\n",
       "      <td>张一山</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3b4bbfef-fbff-4eab-848a-cb9e9fc0f429</td>\n",
       "      <td>宋威龙</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>35778304-a5b8-4186-ac3f-5f41e62dd810</td>\n",
       "      <td>吴亦凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>36583e77-56a3-4e68-8677-60b221e62e56</td>\n",
       "      <td>岳云鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74</td>\n",
       "      <td>王凯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>eb471796-0860-4887-9719-212908594d66</td>\n",
       "      <td>黄轩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9146ef76-b10e-49f6-adc7-6684b04457e8</td>\n",
       "      <td>郑元畅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8704bc6c-39b9-453e-9f6e-1a122b8e5c49</td>\n",
       "      <td>辰亦儒</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4</td>\n",
       "      <td>杜江</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>6146359b-3cbe-4667-a6a4-f549f2d555d6</td>\n",
       "      <td>吴磊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>46cd6146-563e-4b1f-88d9-7d60e3217980</td>\n",
       "      <td>李诞</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>693b6601-2624-4ad8-89f0-d6fd3442b3b9</td>\n",
       "      <td>周深</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   941f4a2c-1029-4d30-a46b-084825dfedba       罗晋\n",
       "1   7f076845-ff16-4aeb-b275-e7a58ac551bc      高伟光\n",
       "2   2c45febb-8c83-4e4f-a033-eba94eb2e12a       胡歌\n",
       "3   fa443cd3-3e80-4e15-b0a7-08282f731859      张一山\n",
       "4   3b4bbfef-fbff-4eab-848a-cb9e9fc0f429      宋威龙\n",
       "5   35778304-a5b8-4186-ac3f-5f41e62dd810      吴亦凡\n",
       "6   36583e77-56a3-4e68-8677-60b221e62e56      岳云鹏\n",
       "7   9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74       王凯\n",
       "8   eb471796-0860-4887-9719-212908594d66       黄轩\n",
       "9   9146ef76-b10e-49f6-adc7-6684b04457e8      郑元畅\n",
       "10  8704bc6c-39b9-453e-9f6e-1a122b8e5c49      辰亦儒\n",
       "11  4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4       杜江\n",
       "12  6146359b-3cbe-4667-a6a4-f549f2d555d6       吴磊\n",
       "13  46cd6146-563e-4b1f-88d9-7d60e3217980       李诞\n",
       "14  693b6601-2624-4ad8-89f0-d6fd3442b3b9       周深"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.io.json.json_normalize(r_get_facelist.json()[\"persistedFaces\"]).drop_duplicates(keep=\"first\", subset=\"userData\")# 升级pandas才能运行\n",
    "df\n",
    "# 重复的数据过多，用drop_duplicates处理一下重复的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>confidence</th>\n",
       "      <th>persistedFaceId</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.14591</td>\n",
       "      <td>6146359b-3cbe-4667-a6a4-f549f2d555d6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.10903</td>\n",
       "      <td>9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.10178</td>\n",
       "      <td>4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.09899</td>\n",
       "      <td>8704bc6c-39b9-453e-9f6e-1a122b8e5c49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.09700</td>\n",
       "      <td>941f4a2c-1029-4d30-a46b-084825dfedba</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.09700</td>\n",
       "      <td>9146ef76-b10e-49f6-adc7-6684b04457e8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.09519</td>\n",
       "      <td>7f076845-ff16-4aeb-b275-e7a58ac551bc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.09388</td>\n",
       "      <td>693b6601-2624-4ad8-89f0-d6fd3442b3b9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.09346</td>\n",
       "      <td>35778304-a5b8-4186-ac3f-5f41e62dd810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.09098</td>\n",
       "      <td>36583e77-56a3-4e68-8677-60b221e62e56</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   confidence                       persistedFaceId\n",
       "0     0.14591  6146359b-3cbe-4667-a6a4-f549f2d555d6\n",
       "1     0.10903  9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74\n",
       "2     0.10178  4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4\n",
       "3     0.09899  8704bc6c-39b9-453e-9f6e-1a122b8e5c49\n",
       "4     0.09700  941f4a2c-1029-4d30-a46b-084825dfedba\n",
       "5     0.09700  9146ef76-b10e-49f6-adc7-6684b04457e8\n",
       "6     0.09519  7f076845-ff16-4aeb-b275-e7a58ac551bc\n",
       "7     0.09388  693b6601-2624-4ad8-89f0-d6fd3442b3b9\n",
       "8     0.09346  35778304-a5b8-4186-ac3f-5f41e62dd810\n",
       "9     0.09098  36583e77-56a3-4e68-8677-60b221e62e56"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdf = pd.io.json.json_normalize(r_findsimilars.json()).drop_duplicates(keep=\"first\", subset=\"persistedFaceId\")# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6146359b-3cbe-4667-a6a4-f549f2d555d6</td>\n",
       "      <td>吴磊</td>\n",
       "      <td>0.14591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74</td>\n",
       "      <td>王凯</td>\n",
       "      <td>0.10903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4</td>\n",
       "      <td>杜江</td>\n",
       "      <td>0.10178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>8704bc6c-39b9-453e-9f6e-1a122b8e5c49</td>\n",
       "      <td>辰亦儒</td>\n",
       "      <td>0.09899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>941f4a2c-1029-4d30-a46b-084825dfedba</td>\n",
       "      <td>罗晋</td>\n",
       "      <td>0.09700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7f076845-ff16-4aeb-b275-e7a58ac551bc</td>\n",
       "      <td>高伟光</td>\n",
       "      <td>0.09519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>693b6601-2624-4ad8-89f0-d6fd3442b3b9</td>\n",
       "      <td>周深</td>\n",
       "      <td>0.09388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35778304-a5b8-4186-ac3f-5f41e62dd810</td>\n",
       "      <td>吴亦凡</td>\n",
       "      <td>0.09346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>36583e77-56a3-4e68-8677-60b221e62e56</td>\n",
       "      <td>岳云鹏</td>\n",
       "      <td>0.09098</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "8  6146359b-3cbe-4667-a6a4-f549f2d555d6       吴磊     0.14591\n",
       "4  9f5f8c1c-04f5-40ff-9c15-eaf0b4280d74       王凯     0.10903\n",
       "7  4dcbd3de-adc3-4558-bb6c-1f3defbb2fe4       杜江     0.10178\n",
       "6  8704bc6c-39b9-453e-9f6e-1a122b8e5c49      辰亦儒     0.09899\n",
       "0  941f4a2c-1029-4d30-a46b-084825dfedba       罗晋     0.09700\n",
       "1  7f076845-ff16-4aeb-b275-e7a58ac551bc      高伟光     0.09519\n",
       "9  693b6601-2624-4ad8-89f0-d6fd3442b3b9       周深     0.09388\n",
       "2  35778304-a5b8-4186-ac3f-5f41e62dd810      吴亦凡     0.09346\n",
       "3  36583e77-56a3-4e68-8677-60b221e62e56      岳云鹏     0.09098"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#result = pd.merge(left, right, how='right', on=['key1', 'key2'])\n",
    "pd.merge(df, bdf,how='right', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False).drop_duplicates(keep=\"first\", subset=\"confidence\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 作业小结\n",
    "* 在做好代码之前，也要讲好自己的故事，扬长避短吧\n",
    "* 闹了个小乌龙，表格运行出来出现重复数据，是原先的代码问题，后来我就找了在用pandas处理数据时用了.drop_duplicates处理重复数据，但是后来原有代码解决了，我也就学会了新的知识点，也让我了解到pandas对于Python来说真的很重要\n",
    "* 这一个月来我的学习成果大概就是战胜恐惧，迎难而上吧！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
